package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.CartAddNewDTO;
import cn.tedu.loding.pojo.vo.CartItemVO;
import cn.tedu.loding.pojo.vo.CartStandardVO;
import cn.tedu.loding.service.ICartService;
import cn.tedu.loding.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理购物车相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/carts")
@Api(tags = "11.购物车管理模块")
public class CartController {

    @Autowired
    private ICartService cartService;

    public CartController() {
        log.debug("创建控制器对象：CartController");
    }

    // http://localhost:9090/carts/add-new?name=购物车001&description=购物车001的简介&sort=199
    @ApiOperation("添加购物车")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<String> addNew(@Valid CartAddNewDTO cartAddNewDTO) {
        log.debug("开始处理【添加购物车】的请求，参数：{}", cartAddNewDTO);
        cartService.addNew(cartAddNewDTO);
        log.debug("添加购物车成功！");
        return JsonResult.ok("添加购物车成功!");
    }

    // http://localhost:9090/carts/9527/delete
    @ApiOperation("根据id删除购物车")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "购物车id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<String> delete(@Range(min = 1, message = "删除购物车失败，尝试删除的购物车的ID无效！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除购物车】的请求，参数：{}", id);
        cartService.delete(id);
        return JsonResult.ok("已删除该购物车及其属性和图片");
    }


    @ApiOperation("根据id查询购物车详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "购物车id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CartItemVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询购物车详情】的请求，参数：{}", id);
        CartItemVO cartItemVO = cartService.getStandardById(id);
        return JsonResult.ok(cartItemVO);
    }


    //localhost:9090/carts
    @ApiOperation("购物车列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<CartItemVO>> List(){
        log.debug("开始处理【购物车列表】请求");
          List<CartItemVO>list = cartService.list();
        return JsonResult.ok(list);
    }
}
